﻿/*---------------------------------------------------------------------------------
-----------------***DaLianQianYi***-----------------------
-----------------作者：大连仟亿科技------------------------------------------------------
-----------------联系作者：www.qianyikeji.com----------------------------------
-----------------时间：2017/10/24 9:19:20------------------------------------------
----------------------------------------------------------------------------------*/


using System;
using System.Collections.Generic;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using Donal.Xms.Common;
using Newtonsoft.Json;

namespace Donal.Xms.Web.qianyi.common
{
    public partial class coupons_edit : Web.UI.ManagePage
    {
        private string action = DTEnums.ActionEnum.Add.ToString(); //操作类型
        protected int id = 0;
        protected int hidConditions = 0;
        protected int hidCoupons_form = 0;
        protected int hidExpiration_form = 0;
        protected int hidCategory = 0;
        BLL.products_category bll = new BLL.products_category();

        protected void Page_Load(object sender, EventArgs e)
        {
            string _action = DTRequest.GetQueryString("action");

            if (!string.IsNullOrEmpty(_action) && _action == DTEnums.ActionEnum.Edit.ToString())
            {
                this.action = DTEnums.ActionEnum.Edit.ToString();//修改类型
                this.id = DTRequest.GetQueryInt("id");
                if (this.id == 0)
                {
                    JscriptMsg("传输参数不正确！", "back");
                    return;
                }
                if (!new BLL.coupons().Exists(this.id))
                {
                    JscriptMsg("记录不存在或已被删除！", "back");
                    return;
                }
            }

            if (!Page.IsPostBack)
            {
                ChkAdminLevel("coupons", DTEnums.ActionEnum.View.ToString()); //检查权限

                if (action == DTEnums.ActionEnum.Edit.ToString()) //修改
                {
                    ShowInfo(this.id);
                }

                txtStart_time.Text = txtStart_time.Text.Trim() == "" ? DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") : txtStart_time.Text.Trim();
                txtEnd_time.Text = txtEnd_time.Text.Trim() == "" ? DateTime.Now.AddDays(7).ToString("yyyy-MM-dd HH:mm:ss") : txtEnd_time.Text.Trim();
            }
        }

        #region 赋值操作=================================
        private void ShowInfo(int _id)
        {
            BLL.coupons bll = new BLL.coupons();
            Model.coupons model = bll.GetModel(_id);

            txtTitle.Text = model.title;
            txtContents.Text = model.contents;
            txtStart_time.Text = DateTime.Parse(model.start_time.ToString()).ToString("yyyy-MM-dd HH:mm:ss");
            txtEnd_time.Text = DateTime.Parse(model.end_time.ToString()).ToString("yyyy-MM-dd HH:mm:ss");
            cbStatus.Checked = model.status == 0 ? true : false;
            txtConditions_amount.Text = model.conditions_amount.ToString();
            txtAmount.Text = model.amount.ToString();
            txtSort_id.Text = model.sort_id.ToString();
            cblExpiration_form.Text = model.expiration_form.ToString();
            txtExpiration_num.Text = model.expiration_num.ToString();
            hidExpiration_form = model.expiration_form;

            rptCategoryList.DataSource = model.product_categories;
            rptCategoryList.DataBind();
        }
        #endregion

        #region 增加操作=================================
        private bool DoAdd()
        {
            Model.coupons model = new Model.coupons();
            BLL.coupons bll = new BLL.coupons();

            model.title = txtTitle.Text;
            model.contents = txtContents.Text;
            model.start_time = DateTime.Parse(txtStart_time.Text);
            model.end_time = DateTime.Parse(txtEnd_time.Text);
            model.status = cbStatus.Checked ? 0 : 1;
            model.amount = decimal.Parse(txtAmount.Text);
            model.sort_id = int.Parse(txtSort_id.Text);
            model.expiration_form = int.Parse(cblExpiration_form.SelectedValue);
            model.conditions_amount = decimal.Parse(txtConditions_amount.Text);
            model.expiration_num = int.Parse(txtExpiration_num.Text);
            model.add_time = DateTime.Now;

            #region 保存适用产品类别====================
            string[] hidItemId = Request.Form.GetValues("hidItemId");

            if (hidItemId != null && hidItemId.Length > 0)
            {
                List<Model.coupons_product_category> ls = new List<Model.coupons_product_category>();
                for (int i = 0; i < hidItemId.Length; i++)
                {
                    ls.Add(new Model.coupons_product_category { category_id = Utils.ObjToInt(hidItemId[i]) });

                }
                model.product_categories = ls;
            }
            #endregion

            if (bll.Add(model) > 0)
            {
                AddAdminLog(DTEnums.ActionEnum.Add.ToString(), "添加优惠券成功 " + JsonConvert.SerializeObject(model)); //记录日志
                return true;
            }
            return false;
        }
        #endregion

        #region 修改操作=================================
        private bool DoEdit(int _id)
        {
            bool result = false;
            BLL.coupons bll = new BLL.coupons();
            Model.coupons model = bll.GetModel(_id);

            model.title = txtTitle.Text;
            model.contents = txtContents.Text;
            model.start_time = DateTime.Parse(txtStart_time.Text);
            model.end_time = DateTime.Parse(txtEnd_time.Text);
            model.status = cbStatus.Checked ? 0 : 1;
            model.amount = decimal.Parse(txtAmount.Text);
            model.sort_id = int.Parse(txtSort_id.Text);
            model.expiration_form = int.Parse(cblExpiration_form.SelectedValue);
            model.conditions_amount = decimal.Parse(txtConditions_amount.Text);
            model.expiration_num = int.Parse(txtExpiration_num.Text);


            #region 保存适用产品类别====================
            string[] hidItemId = Request.Form.GetValues("hidItemId");

            if (hidItemId != null && hidItemId.Length > 0)
            {
                List<Model.coupons_product_category> ls = new List<Model.coupons_product_category>();
                for (int i = 0; i < hidItemId.Length; i++)
                {
                    ls.Add(new Model.coupons_product_category
                    {
                        coupons_id = model.id,
                        category_id = Utils.ObjToInt(hidItemId[i])
                    });

                }
                model.product_categories = ls;
            }
            #endregion

            if (bll.Update(model))
            {
                AddAdminLog(DTEnums.ActionEnum.Edit.ToString(), "修改优惠券成功 " + JsonConvert.SerializeObject(model)); //记录日志
                result = true;
            }

            return result;
        }
        #endregion       

        //保存
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (action == DTEnums.ActionEnum.Edit.ToString()) //修改
            {
                ChkAdminLevel("coupons", DTEnums.ActionEnum.Edit.ToString()); //检查权限
                if (!DoEdit(this.id))
                {
                    JscriptMsg("保存过程中发生错误！", string.Empty);
                    return;
                }
                JscriptMsg("修改优惠卷成功！", "coupons_list.aspx");
            }
            else //添加
            {
                ChkAdminLevel("coupons", DTEnums.ActionEnum.Add.ToString()); //检查权限
                if (!DoAdd())
                {
                    JscriptMsg("保存过程中发生错误！", string.Empty);
                    return;
                }
                JscriptMsg("添加优惠卷成功！", "coupons_list.aspx");
            }
        }

    }
}

